<?php

class NavgationController extends BackendController
{
    public $active = "navgation";

    public function actionIndex()
    {

        $action_type = isset($_REQUEST['action_type']) ? $_REQUEST['action_type'] : null;
        if ($action_type == 'insert') {
            $navgation_text = $_REQUEST['navgation_text'];
            $navgation_type = $_REQUEST['navgation_type'];
            $navgation_order = $_REQUEST['navgation_order'];
            $navgation_class = $_REQUEST['navgation_class'];
            $this->connection->createCommand("insert into rk_navgation (navgation_text,navgation_type,navgation_order,navgation_class) values (
            :navgation_text,:navgation_type,:navgation_order,:navgation_class
            )")
                ->bindParam(":navgation_text", $navgation_text)
                ->bindParam(":navgation_type", $navgation_type)
                ->bindParam(":navgation_order", $navgation_order)
                ->bindParam(":navgation_class", $navgation_class)
                ->execute();
            $insertId = $this->connection->getLastInsertID();
            $this->insertMeta($navgation_type,$insertId);
        } else if ($action_type == 'delete') {
            $navgation_id = $_REQUEST['navgation_id'];
            $this->connection->createCommand("delete from rk_navgation where navgation_id = :navgation_id")
                ->bindParam(":navgation_id", $navgation_id)
                ->execute();
            $this->connection->createCommand("delete from rk_navgationmeta where navgation_id = :navgation_id")
                ->bindParam(":navgation_id", $navgation_id)
                ->execute();
        } else if ($action_type == 'update') {
            $navgation_id = $_REQUEST['navgation_id'];
            $editNavgation = $this->connection->createCommand("select * from rk_navgation where navgation_id = :navgation_id")
                ->bindParam(":navgation_id", $navgation_id)
                ->queryRow();
            $metaList = $this->connection->createCommand("select * from rk_navgationmeta where navgation_id = :navgation_id")
                ->bindParam(":navgation_id", $navgation_id)
                ->queryAll();
            $editNavgationMeta = array();
            foreach ($metaList as $item) {
                $editNavgationMeta[$item['meta_key']] = $item['meta_value'];
            }
            $this->data['editNavgation'] = $editNavgation;
            $this->data['editNavgationMeta'] = $editNavgationMeta;
        } else if ($action_type == 'updatedo') {
            $navgation_id = $_REQUEST['navgation_id'];
            $navgation_text = $_REQUEST['navgation_text'];
            $navgation_type = $_REQUEST['navgation_type'];
            $navgation_order = $_REQUEST['navgation_order'];
            $navgation_class = $_REQUEST['navgation_class'];

            $this->connection->createCommand("update rk_navgation set navgation_text = :navgation_text,navgation_type=:navgation_type,
navgation_order=:navgation_order,navgation_class=:navgation_class where navgation_id = :navgation_id")
                ->bindParam(":navgation_text", $navgation_text)
                ->bindParam(":navgation_type", $navgation_type)
                ->bindParam(":navgation_order", $navgation_order)
                ->bindParam(":navgation_id", $navgation_id)
                ->bindParam(":navgation_class", $navgation_class)
                ->execute();

            $this->connection->createCommand("delete from rk_navgationmeta where navgation_id = :navgation_id")
                ->bindParam(":navgation_id", $navgation_id)
                ->execute();

            $this->insertMeta($navgation_type,$navgation_id);

        }

        $categoryOfArticle = $this->connection->createCommand("select * from rk_category where category_type = 'article'")->queryAll();
        $this->data['categoryOfArticle'] = $categoryOfArticle;
        $categoryOfImage = $this->connection->createCommand("select * from rk_category where category_type = 'image'")->queryAll();
        $this->data['categoryOfImage'] = $categoryOfImage;
        $categoryOfFile = $this->connection->createCommand("select * from rk_category where category_type = 'file'")->queryAll();
        $this->data['categoryOfFile'] = $categoryOfFile;

        $navgations = $this->connection->createCommand("select * from rk_navgation order by navgation_order desc")->queryAll();
        $this->data['navgations'] = $navgations;

        $this->render("index", $this->data);
    }

    private function insertMeta($navgation_type,$insertId){
        if ($navgation_type == 'link') {
            $link_address = $_REQUEST['link_address'];
            $this->connection->createCommand("insert into rk_navgationmeta (navgation_id,meta_key,meta_value) values (:navgation_id,'link_address',:meta_value)")
                ->bindParam(":navgation_id", $insertId)
                ->bindParam(":meta_value", $link_address)
                ->execute();
        } else if ($navgation_type == 'article') {
            $type_article = $_REQUEST['type_article'];
            $this->connection->createCommand("insert into rk_navgationmeta (navgation_id,meta_key,meta_value) values (:navgation_id,'category_id',:meta_value)")
                ->bindParam(":navgation_id", $insertId)
                ->bindParam(":meta_value", $type_article)
                ->execute();
        } else if ($navgation_type == 'image') {
            $type_image = $_REQUEST['type_image'];
            $this->connection->createCommand("insert into rk_navgationmeta (navgation_id,meta_key,meta_value) values (:navgation_id,'category_id',:meta_value)")
                ->bindParam(":navgation_id", $insertId)
                ->bindParam(":meta_value", $type_image)
                ->execute();
        } else if ($navgation_type == 'file') {
            $type_file = $_REQUEST['type_file'];
            $this->connection->createCommand("insert into rk_navgationmeta (navgation_id,meta_key,meta_value) values (:navgation_id,'category_id',:meta_value)")
                ->bindParam(":navgation_id", $insertId)
                ->bindParam(":meta_value", $type_file)
                ->execute();
        }
    }
}